<template>
  <a-modal
    v-model:visible="visible"
    :title="title"
    :mask-closable="false"
    :esc-to-close="false"
    width="700px"
    @before-ok="save"
    @close="reset"
  >
    <GiForm ref="formRef" v-model="form" :options="options" :columns="columns" />
  </a-modal>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import { addEquipment, getEquipment, updateEquipmentName } from '@/apis/organization/equipment'
import { getBuildingList, getFactoryList, getMeetingRoomList } from '@/apis/common'
import { type Columns, GiForm, type Options } from '@/components/GiForm'
import { useForm } from '@/hooks'

const emit = defineEmits<{
  (e: 'save-success'): void
}>()

const dataId = ref('')
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改会议终端' : '新增会议终端'))
const formRef = ref<InstanceType<typeof GiForm>>()

const options: Options = {
  form: {
    rules: {
      name: [{ required: true, message: '终端设备名称不能为空!' }],
      factorySiteId: [{ required: true, message: '所属厂区不能为空!' }],
      officeBuildingId: [{ required: true, message: '所属办公楼不能为空!' }],
      meetingRoomId: [{ required: true, message: '所属会议室不能为空!' }]
    }
  },
  btns: { hide: true }
}

const { form, resetForm } = useForm({})

const columns = computed<Columns<typeof form>>(() => [
  {
    label: '终端设备名称',
    field: 'name',
    type: 'input',
    span: 24
  },
  {
    label: '所属厂区',
    field: 'factorySiteId',
    type: 'select',
    init: true,
    cascader: ['officeBuildingId'],
    request: getFactoryList,
    resultFormat(res) {
      return res.data.map((item) => {
        return {
          value: item.id,
          label: item.name
        }
      })
    },
    span: 24
  },
  {
    label: '所属办公楼',
    field: 'officeBuildingId',
    type: 'select',
    init: false,
    cascader: ['meetingRoomId'],
    request: getBuildingList,
    resultFormat(res) {
      return res.data.map((item) => {
        return {
          value: item.id,
          label: item.name
        }
      })
    },
    span: 24
  },
  {
    label: '所属会议室',
    field: 'meetingRoomId',
    type: 'select',
    init: false,
    request: getMeetingRoomList,
    resultFormat(res) {
      return res.data.map((item) => {
        return {
          value: item.id,
          label: item.name
        }
      })
    },
    span: 24
  }
])

// 重置
const reset = () => {
  formRef.value?.formRef?.resetFields()
  resetForm()
}

const visible = ref(false)
// 新增
const onAdd = () => {
  reset()
  dataId.value = ''
  visible.value = true
}

// 修改
const onUpdate = async (id: string) => {
  console.log(id, 'id')
  reset()
  dataId.value = id
  const res = await getEquipment(id)
  console.log(res.data, 'res.data')
  Object.assign(form, res.data)
  visible.value = true
}

// 保存
const save = async () => {
  try {
    const isInvalid = await formRef.value?.formRef?.validate()
    if (isInvalid) return false
    if (isUpdate.value) {
      await updateEquipmentName({
        id: Number(dataId.value),
        name: form.name,
        meetingRoomId: form.meetingRoomId
      })
      Message.success('修改成功')
    } else {
      await addEquipment(form)
      Message.success('新增成功')
    }
    emit('save-success')
    return true
  } catch (error) {
    return false
  }
}

defineExpose({ onAdd, onUpdate })
</script>
